package com.heima.common.minio.config;

import com.heima.common.minio.properties.MinIOProperties;
import com.heima.common.minio.service.MinIOService;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author: itheima
 * @create: 2022-07-29 10:30
 */
@Configuration
@EnableConfigurationProperties(MinIOProperties.class)  //将属性类对象加入到IOC容器
@Import(MinIOService.class)  //将业务类对注册到IOC容器中
//加个“开关”=是否满足加载条件
@ConditionalOnProperty(prefix = "minio", value = "enable", havingValue = "true")
public class MinIoConfig {


    /**
     * 将访问MIniO客户端注册IOC容器中
     *
     * @param minIOProperties
     * @return
     */
    @Bean
    public MinioClient minioClient(MinIOProperties minIOProperties) {
        // 使用MinIO服务的URL，端口，Access key和Secret key创建一个MinioClient对象
        MinioClient minioClient = MinioClient.builder()
                .endpoint(minIOProperties.getEndpoint())
                .credentials(minIOProperties.getAccessKey(), minIOProperties.getSecretKey())
                .build();
        return minioClient;
    }
}
